<?php

class Pagination_model extends CI_Model {

    //put your code here

    public $order = 'ASC';
    public $action = '';
    public $limit = 20;
    public $offset = 0;
    public $total_records = 0;
    public $page = 0;
    public $no_of_pages = 0;
    public $page_options = '';
    public $array_pages = array();
    public $next = '';
    public $prev = '';
    public $status = 'active';

    function __construct() {

        parent::__construct();
    }

    private function page_options() {
        for ($i = 0; $i < $this->no_of_pages; $i++) {
            $dis = $i + 1;
            $sel = '';
            $this->array_pages[] = $dis;
            if ($this->page == $i)
                $sel = 'selected';
            $this->page_options .= "<option value=\"$i\" $sel >$dis</option>\n";
        }
    }

    private function total_records($param) {
        extract($param);
        $this->db->select(array('COUNT(*) as total'));
        $this->db->from($from);

        if (isset ($where)) {
            $this->db->where($where);
        }

        $res = $this->db->get()->row_array();
        $this->total_records = $res['total'];
        return $this->total_records;
    }

    private function offset() {
        $this->offset = $this->limit * $this->page;
        return $this->offset;
    }

    private function no_of_pages() {
        $this->no_of_pages = $this->total_records / $this->limit;
        return $this->no_of_pages;
    }

    private function set_next_prev() {
        if ($this->page == 0)
            $this->prev = 'disabled';
        if ($this->page >= ($this->no_of_pages - 1))
            $this->next = 'disabled';
    }

    public function set_param($param) {
        extract($param);
        if ($query) {
            $this->total_records($query);
        }
        if ($page) {
            $this->page = $param['page'];
        }
        if ($limit) {
            $this->limit = $param['limit'];
        }
        if ($order) {
            $this->order = $param['order'];
        }
        if ($action) {
            $this->action = $action;
        }
        if ($status) {
            $this->status = $status;
        }

        $this->no_of_pages();

        if ($this->page >= $this->no_of_pages) {
            $this->page = 0;
        }
        $this->offset();

        $this->page_options();
        $this->set_next_prev();
    }
    
    function result_array(){
        
        $public = array('order','action','limit','offset','total_records','page','no_of_pages','page_options','array_pages','next','prev','status');
        $ret = array();
        foreach ($public as $v){
            $ret[$v] = $this->$v;
        }
        return $ret;
    
    }

}

?>
